package com.mysiteforme.admin.controller.scrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ScrapTarget;
import com.mysiteforme.admin.service.scrap.ScrapTargetService;
import com.mysiteforme.admin.util.other.LayerData;
import com.mysiteforme.admin.util.other.RestResponse;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;
import javax.servlet.ServletRequest;
import java.util.Map;

/**
 * <p>
 * 报废目标  前端控制器
 * </p>
 */
@Controller
@RequestMapping("/admin/scrapTarget")
public class ScrapTargetController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScrapTargetController.class);

    @Autowired
    private ScrapTargetService scrapTargetService;

    @GetMapping("list")
    @SysLog("跳转报废目标列表")
    public String list(){
        return "/admin/scrapTarget/list";
    }
    @RequiresPermissions("scrap:target:list")
    @PostMapping("list")
    @ResponseBody
    @SysLog("请求报废目标列表数据")
    public LayerData<ScrapTarget> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ScrapTarget> layerData = new LayerData<>();
        EntityWrapper<ScrapTarget> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String scrapProject = (String) map.get("scrapProject");
            if(StringUtils.isNotBlank(scrapProject)) {
                wrapper.like("scrap_project",scrapProject);
            }else{
                map.remove("scrapProject");
            }

        }
        wrapper.orderBy("sort", false);
        Page<ScrapTarget> pageData = scrapTargetService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增报废目标页面")
    public String add(){
        return "/admin/scrapTarget/add";
    }
    
    @RequiresPermissions("scrap:target:add")
    @PostMapping("add")
    @SysLog("保存新增报废目标数据")
    @ResponseBody
    public RestResponse add(ScrapTarget scrapTarget){
        scrapTargetService.insert(scrapTarget);
        return RestResponse.success();
    }
    
    @GetMapping("edit")
    @SysLog("跳转编辑报废目标页面")
    public String edit(Long id,Model model){
        ScrapTarget scrapTarget = scrapTargetService.selectById(id);
        model.addAttribute("scrapTarget",scrapTarget);
        return "/admin/scrapTarget/edit";
    }
    
    @RequiresPermissions("scrap:target:edit")
    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑报废目标数据")
    public RestResponse edit(ScrapTarget scrapTarget){
        if(null == scrapTarget.getId() || 0 == scrapTarget.getId()){
            return RestResponse.failure("ID不能为空");
        }
        scrapTargetService.updateById(scrapTarget);
        return RestResponse.success();
    }
    @RequiresPermissions("scrap:target:delete")
    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除报废目标数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ScrapTarget scrapTarget = scrapTargetService.selectById(id);
        scrapTarget.setDelFlag(true);
        scrapTargetService.updateById(scrapTarget);
        return RestResponse.success();
    }

}